πŸ‘©β€πŸ’» 17678. μ…”ν‹€λ²„μŠ€

@Seongukbaek Posted May 20, 2022 Β· Updated May 20, 2022 Β· 3 min read

문제

Programmers - μ…”ν‹€λ²„μŠ€

πŸ“ Logic

public int rideBus() { int departBus = 540; for (int i = 0; i < n; i++) { while(!crueTimes.isEmpty()) { int crue = crueTimes.peek(); if (departBus >= crue && busTimes.get(i).size() < m) { crueTimes.poll(); busTimes.get(i).add(crue); lastCrue = crue - 1; } else break; } departBus += t; } if (busTimes.get(n - 1).size() < m) lastCrue = departBus - t; return lastCrue; }
  • departBus 둜 λ²„μŠ€ 좜발 μ‹œκ°μ„ μ„ΈνŒ…ν•˜κ³ , μš°μ„ μˆœμœ„ 큐에 μ €μž₯된 ν¬λ£¨λ“€μ˜ λŒ€κΈ°μ—΄ 도착 μ‹œκ°„μ„ ν•˜λ‚˜μ”© λΉ„κ΅ν•˜λ©° νƒ‘μŠΉν•  수 μžˆλŠ” 경우 νμ—μ„œ μ œμ™Έν•œλ‹€.
  • μ΄λ•Œ λ§ˆμ§€λ§‰μœΌλ‘œ νƒ‘μŠΉν•œ μΈμ›μ˜ λŒ€κΈ°μ—΄ 도착 μ‹œκ°„μ„ μ•Œμ•„μ•Ό ν•˜κΈ°μ— lastCrue 에 μ €μž₯ν•œλ‹€. (μ΄λ•Œ -1ν•œ 값을 μ €μž₯)
  • μ½˜μ€ λ§ˆμ§€λ§‰ λ²„μŠ€μ—λ§Œ 타면 λ˜λ―€λ‘œ, λ§ˆμ§€λ§‰ λ²„μŠ€κ°€ λ§Œμ„μΈμ§€ ν™•μΈν•˜μ—¬

    • λ§Œμ„μΈ 경우, lastCrue λ₯Ό λ°˜ν™˜ν•˜κ³ ,
    • λ§Œμ„μ΄ μ•„λ‹Œ 경우, ν•΄λ‹Ή λ²„μŠ€μ˜ 좜발 μ‹œκ°μ„ λ°˜ν™˜ν•œλ‹€.

πŸ“„ CODE

μ½”λ“œ 보기/μ ‘κΈ°πŸ’«
import java.util.*; class Shuttle { Queue<Integer> crueTimes; ArrayList<ArrayList<Integer>> busTimes; int n; int t; int m; int lastCrue; public Shuttle(int n, int t, int m) { this.n = n; this.t = t; this.m = m; busTimes = new ArrayList<>(); crueTimes = new PriorityQueue<>(); initBusTimes(); } private void initBusTimes() { for (int i = 0; i < n; i++) busTimes.add(new ArrayList<>()); } public void fillCrue(String[] timetable) { for (String tt : timetable) { String[] tmp = tt.split(":"); crueTimes.add(Integer.parseInt(tmp[0]) * 60 + Integer.parseInt(tmp[1])); } } public int rideBus() { int departBus = 540; for (int i = 0; i < n; i++) { while(!crueTimes.isEmpty()) { int crue = crueTimes.peek(); if (departBus >= crue && busTimes.get(i).size() < m) { crueTimes.poll(); busTimes.get(i).add(crue); lastCrue = crue - 1; } else break; } departBus += t; } if (busTimes.get(n - 1).size() < m) lastCrue = departBus - t; return lastCrue; } } class Solution { public String solution(int n, int t, int m, String[] timetable) { Shuttle st = new Shuttle(n, t, m); st.fillCrue(timetable); StringBuilder answer = new StringBuilder(); int time = st.rideBus(); int hour = time/60; int min = time%60; if (hour < 10) answer.append("0"); answer.append(hour).append(":"); if (min < 10) answer.append("0"); answer.append(min); return answer.toString(); } }

✏️ Review

  • λ²„μŠ€λ§ˆλ‹€ νƒˆ 수 μžˆλŠ” 인원을 λͺ¨λ‘ μ±„μ›Œλ†“κ³ , 맨 λ§ˆμ§€λ§‰ λ²„μŠ€μ— λŒ€ν•΄μ„œλ§Œ ν™•μΈν•˜μ—¬ 제일 λŠ¦μ€ μ‹œκ°μ„ κ΅¬ν•˜λ €κ³  ν–ˆλŠ”λ°, ν…ŒμΌ€ 14 ~ 16번이 죽어도 μ‹€νŒ¨μ˜€λ‹€.
  • μƒκ°λ‚˜λŠ” ν…ŒμΌ€λŠ” λ‹€ λ§Œλ“€μ–΄μ„œ 해봐도 λ„μ €νžˆ 뭐가 μ•ˆ λ§žλŠ” 건지 λͺ¨λ₯΄κ² μ–΄μ„œ λ‹€λ₯Έ 풀이λ₯Ό λ΄€λ‹€.
  • λˆ„κ°€ μ’€ μ†μ‹œμ›ν•˜κ²Œ μ•Œλ €μ€¬μœΌλ©΄ μ’‹κ² λ‹€ ...
@Seongukbaek
배우고 μ •λ¦¬ν•˜κ³  κ³΅μœ ν•˜λŠ” λŒ€ν•™μƒ λ°±μ„±μš±μž…λ‹ˆλ‹€ πŸ˜€